package com.huishu.goods.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huishu.commons.pojo.Result;
import com.huishu.commons.pojo.dto.DeductionStockDTO;
import com.huishu.commons.pojo.vo.CartItemGoodsVO;
import com.huishu.goods.pojo.dto.CategoryProductDTO;
import com.huishu.goods.pojo.dto.ProductCreateDTO;
import com.huishu.goods.pojo.dto.ProductPageDTO;
import com.huishu.goods.pojo.dto.ProductUpdateDTO;
import com.huishu.goods.pojo.model.ProductDO;
import com.huishu.goods.pojo.model.ProductGiveDO;
import com.huishu.goods.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author generator@ljj
 * @since 2023-04-08
 */
@RestController
@RequestMapping("/product")
@Slf4j
public class ProductController {

    @Autowired
    private ProductService productService;

    @PostMapping("/category-product")
    public Result categoryProduct(@RequestBody @Valid CategoryProductDTO categoryProductDTO) {
        log.info("获取品类商品:{}", JSON.toJSONString(categoryProductDTO));
        return productService.categoryProduct(categoryProductDTO);
    }

    @PostMapping("/find-new-product")
    public Result findNewProduct(@RequestBody @Valid CategoryProductDTO categoryProductDTO) {
        log.info("查询重磅新品:{}", JSON.toJSONString(categoryProductDTO));
        return productService.findNewProductOrBestsellingOrSample(categoryProductDTO);
    }

    @PostMapping("/find-bestselling")
    public Result findBestselling(@RequestBody @Valid CategoryProductDTO categoryProductDTO) {
        log.info("查询畅销经典:{}", JSON.toJSONString(categoryProductDTO));
        return productService.findNewProductOrBestsellingOrSample(categoryProductDTO);
    }

    @PostMapping("/find-sample")
    public Result findSample(@RequestBody @Valid CategoryProductDTO categoryProductDTO) {
        log.info("查询样品:{}", JSON.toJSONString(categoryProductDTO));
        return productService.findNewProductOrBestsellingOrSample(categoryProductDTO);
    }

    @PostMapping("/search")
    public Result search(@RequestBody @Valid CategoryProductDTO categoryProductDTO) {
        log.info("搜索商品:{}", JSON.toJSONString(categoryProductDTO));
        return productService.search(categoryProductDTO);
    }

    @GetMapping("/detailed/{id}")
    public Result detailed(@PathVariable Long id) {
        log.info("查询商品详情");
        return productService.detailed(id);
    }

    @GetMapping("/getCouponByGoods/{id}")
    public Result getCouponByGoods(@PathVariable Long id){
        return productService.getCouponByGoods(id);
    }

    @PostMapping("/findCartItemGoods")
    public Result<List<CartItemGoodsVO>> findCartItemGoods(@RequestBody List<Long> ids) {
        return productService.findCartItemGoods(ids);
    }

    @PostMapping("/deduction-stock")
    public Result deductionStock(@RequestBody List<DeductionStockDTO> deductionStockDTOS) {
        log.info("扣减库存:{}", JSON.toJSONString(deductionStockDTOS));
        return productService.deductionStock(deductionStockDTOS);
    }

    @GetMapping("/findAll")
    public Result findAll() {
        LambdaQueryWrapper<ProductDO> queryWrapper = Wrappers.lambdaQuery(ProductDO.class)
                .eq(ProductDO::getIsVerify, 1)
                .eq(ProductDO::getIsDelete, 0)
                .eq(ProductDO::getIsPublish, 1);
        return Result.success(productService.list(queryWrapper));
    }


    /*-------------------------------------------------------------------------------------------------*/
    @PostMapping("/create")
    public Result create(@RequestBody ProductCreateDTO productCreateDTO) {
        log.info("添加商品：{}", JSON.toJSONString(productCreateDTO));
        return productService.create(productCreateDTO);
    }


    @PostMapping("/update")
    public Result modify(@RequestBody ProductUpdateDTO productUpdateDTO) {
        log.info("修改商品：{}", JSON.toJSONString(productUpdateDTO));
        return productService.modify(productUpdateDTO);
    }

    @PostMapping("/set-recommen")
    public Result setRecommen(@RequestBody ProductUpdateDTO productUpdateDTO) {
        log.info("修改推荐商品：{}", JSON.toJSONString(productUpdateDTO));
        return productService.setRecommen(productUpdateDTO);
    }

    @GetMapping("/binding-coupon/{id}/{couponId}")
    public Result bindingCoupon(@PathVariable Long id, @PathVariable Long couponId) {
        return productService.bindingCoupon(id, couponId);
    }

    @GetMapping("/delete-coupon/{id}/{couponId}")
    public Result deleteCoupon(@PathVariable Long id, @PathVariable Long couponId) {
        return productService.deleteCoupon(id, couponId);
    }

    @PostMapping("/pageList")
    public Result pageList(@RequestBody ProductPageDTO productPageDTO) {
        log.info("pageList：{}", JSON.toJSONString(productPageDTO));
        return productService.pageList(productPageDTO);
    }

    @PostMapping("/up-or-down")
    public Result upOrDown(@RequestBody ProductUpdateDTO productUpdateDTO) {
        LambdaUpdateWrapper<ProductDO> uw = Wrappers.lambdaUpdate(ProductDO.class)
                .set(ProductDO::getIsPublish, productUpdateDTO.getIsPublish())
                .eq(ProductDO::getId, productUpdateDTO.getId());
        return productService.update(uw) ? Result.success() : Result.fail();
    }

    @GetMapping("/find-recommen-info/{id}")
    public Result findRecommenInfo(@PathVariable Long id) {
        return productService.findRecommenInfo(id);
    }

    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable Long id) {
        return productService.findById(id);
    }

    @GetMapping("/findByCategoryId/{categoryId}")
    public Result findByCategoryId(@PathVariable Long categoryId) {
        return productService.findByCategoryId(categoryId);
    }

    @GetMapping("/find-binding-coupon/{id}")
    public Result findCouponByProductId(@PathVariable Long id) {
        return productService.findCouponByProductId(id);
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id) {
        return productService.delete(id);
    }

    @GetMapping("/find-give-all")
    public Result findGiveAll() {
        return productService.findGiveAll();
    }

    @PostMapping("/give")
    public Result give(@RequestBody List<ProductGiveDO> productGives) {
        return productService.give(productGives);
    }

    @GetMapping("/find-give/{id}")
    public Result findGive(@PathVariable Long id) {
        return productService.findGive(id);
    }

    @PostMapping("/findById/{productId}")
    public Result findGoodsById(@PathVariable Long productId) {
        return Result.success(productService.getById(productId));
    }

    @GetMapping("/find-cart-recommen")
    public Result findCartRecommen(){
        return productService.findCartRecommen();
    }
}
